<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 24.04.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class clientNodeEstate
    ****
    * This class represents a estate page for any solution
    ****
    * @package clientNodes
    * @author  <tb@essentialmind.com>
    * @version n/a
    * @since 28.05.2011
    * @copyright essentialmind.com 2011
    ***/
    class clientNodeEstate extends clientNode{

        /**
         * @brief representes allowed rooms
         */
        private $rooms_array = array(1 => 1,2,3,4,5,6,7,8,9);


        /**
         * Process inputs
         * 
         * @return void 
         **/
        public function process(){
            

            $estate = $this->db->getRow("SELECT    ne.*,
                                                   ew.name AS window,
                                                   ef.name AS floor,
                                                   ea.name AS accountrement,
                                                   eh.name AS heating,
                                                   et.name AS type,
                                                   ek.name as kind,
                                                   es.name AS state,
                                                   efd.name AS federal
                                          FROM pagemap 
                                          LEFT JOIN node_estate ne ON pagemap.pageid = ne.id 
                                          LEFT JOIN estate_windows ew ON ew.id = ne.window_id
                                          LEFT JOIN estate_floors ef ON ef.id = ne.floor_id
                                          LEFT JOIN estate_accountrements ea ON ea.id = ne.accountrements_id
                                          LEFT JOIN estate_heatings eh ON eh.id = ne.heating_id
                                          LEFT JOIN estate_types et ON et.id = ne.type_id
                                          LEFT JOIN estate_kinds ek ON ek.id = ne.kind_id
                                          LEFT JOIN estate_federals efd ON efd.id = ne.federal_id
                                          LEFT JOIN estate_states es ON es.id = ne.state_id
                                          WHERE ne.id = " . $this->object);
            
            $estate['title'] = $this->db->getOne("SELECT title FROM pagemap WHERE id = ".$this->reference);                                                  
                                                                
            $this->smarty->assign('data',$estate);
            
            $this->smarty->assign('federals', $this->db->getAssoc("SELECT id, name FROM estate_federals ORDER BY id"));
            $this->smarty->assign('types', $this->db->getAssoc("SELECT id, name FROM estate_types ORDER BY id"));
            $this->smarty->assign('kinds', $this->db->getAssoc("SELECT id, name FROM estate_kinds ORDER BY id"));
            $this->smarty->assign('floors', $this->db->getAssoc("SELECT id, name FROM estate_floors ORDER BY id"));
            $this->smarty->assign('windows', $this->db->getAssoc("SELECT id, name FROM estate_windows ORDER BY id"));
            $this->smarty->assign('heatings', $this->db->getAssoc("SELECT id, name FROM estate_heatings ORDER BY id"));
            $this->smarty->assign('states', $this->db->getAssoc("SELECT id, name FROM estate_states ORDER BY id"));
            $this->smarty->assign('accountrements', $this->db->getAssoc("SELECT id, name FROM estate_accountrements ORDER BY id"));
            $this->smarty->assign('rooms', $this->rooms_array);   
            $this->smarty->assign('storeys', $this->storey_array);   
            $this->smarty->assign('baths', $this->accountrements_array);   
            $this->smarty->assign('toilets', $this->accountrements_array);   
            $this->smarty->assign('terraces', $this->accountrements_array);   
            $this->smarty->assign('balconies', $this->accountrements_array);
            $this->smarty->assign('loggias', $this->accountrements_array);  
            $this->smarty->assign('categories', $this->category_array);
            
            $this->smarty->assign('pictures', $this->db->getAll("SELECT ep.* 
                                                                    FROM pagemap pm 
                                                                    JOIN estate_pictures ep ON pm.pageid = ep.estate_id 
                                                                    WHERE pm.id = " .$this->reference . "
                                                                    ORDER BY order_pos ASC"));
            
            $this->smarty->assign('coor', $this->geocode($estate['address'], $estate['zip'], $estate['city']));
        }

        /**
         * geocode
         *
         * Use Google's API Services to get the coordinations of a street address. The returned hash will contain
         * longitude, latitude, altitude and the return code of the request.
         *
         * @see http://code.google.com/apis/maps/documentation/webservices/index.html
         * @return Hash 
         */ 
        public function geocode($address, $zip, $city ,$country = "Austria") {
                //try to geocode
                $address = "http://maps.google.com/maps/geo?q="
                        .urlencode($address)
                        .",+".urlencode($city)
                        .",+".urlencode($zip)
                        .",+".urlencode($country)
                        ."&output=xml&key=ABQIAAAAWXp2xo7gr1swfMpQGmACThSTLV2xOFFsCoBo2LyAuRrMQL34FxQYqMv5OAHXGOzUn4ndOmQQE4fi2w";
error_log($address);
                try{
                        // Retrieve the URL contents
                        $page = file_get_contents($address);
                        $page = iconv('ISO-8859-1', 'UTF-8', $page);
                                
                        // Parse the returned XML file
                        $xml = new SimpleXMLElement($page);
                                
                        // Parse the coordinate string
                        list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates);
                }catch(ExceptionÊ$e){
                        // :)
                }

                return array("long" => $longitude,
                             "lati" => $latitude,
                             "alti" => $altitude,
                             "succ" => $xml->Response->Status->code);
        }

        /**
         * set the action template to estate.html
         * 
         * @return void
         **/
        public function setTemplate(){
            $this->setActionTemplate('estate.html');
        }
    }
?>